package com.itheima.web.controller;

import com.itheima.domain.User;
import com.itheima.domain.UserVO;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 操作User的controller对象
 *
 * 专门用于与浏览器交互，接收请求，返回响应
 */
@RestController
@Slf4j
public class UserController {

    /*spring会自动注入对象*/
    @Autowired
    private UserService userService;

    //根据id查询
    @GetMapping("/selectById")
    public User selectById(Integer id) {
        //调用service完成功能
        User user = userService.selectById(id);
        return user;
    }

    //查询所有，根据age排序
    @GetMapping("/selectOrderByAge")
    public String selectOrderByAge() {
        //调用service完成功能
        List<User> users = userService.selectOrderByAge();

        //处理数据，用于返回页面
        String result = "";

        //循环数据
        for (int i = 0; i < users.size(); i++) {
            result += (i+1);
            User user = users.get(i);
            result += "姓名："+user.getUsername() + ",年龄："+user.getAge();
            result += " ||||||| ";
        }

        return result;
    }

    @GetMapping("/selectByCondition")
    //条件查询，根据电话和名称完成数据过滤
    public String selectByCondition(String phone,String username) {

        //接收数据，将条件转成一个带条件的User对象
        User conditionUser = new User();
        conditionUser.setUsername(username);
        conditionUser.setPhone(phone);

        //调用service
        List<User> usersByCondition = userService.selectByCondition(conditionUser);

        //滤空判断
        if(usersByCondition == null || usersByCondition.size() == 0) {
            return "没有您索要的数据！";
        }

        //处理数据，用于返回页面
        String result = "";

        //循环数据
        for (int i = 0; i < usersByCondition.size(); i++) {
            result += (i+1);
            User user = usersByCondition.get(i);
            result += "姓名："+user.getUsername() + ",年龄："+user.getAge();
            result += " ||||||| ";
        }

        return result;
    }
//    用户名:<input type="text" name="username"> <br>
//    密码:<input type="text" name="password"> <br>
//    年龄:<input type="text" name="age"> <br>
//    电话:<input type="text" name="phone"> <br>
//    生日:<input type="date" name="birthday"> <br>
    //添加用户
    @GetMapping("/addUser")
    public String addUser(UserVO userVO) throws ParseException {
        System.out.println(userVO);
        //将VO对象转成普通的user对象
        User user = userVO.userVO2User();
        System.out.println(user);
        //调用service完成添加
        userService.addUser(user);
        return "添加成功！";
    }

    @GetMapping("/delUser")
    //删除指定id的用户
    public String delUser(Integer delId) {

        //调用service删除，返回是否产出成功
        boolean delSuccess = userService.delUserById(delId);
        if(delSuccess) {
            return "删除成功！";
        }else {
            return "删除失败！id不存在！";
        }
    }
    //修改指定的用户(目前无法完成：为什么？)
}
